﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;

namespace System.Web.UI
{
    public class ZMPageEdit<TModel> : ZMPageEdit<BaseBLL<TModel>, TModel>
        where TModel : BaseModel, new()
    { }
    public class ZMPageEdit<TBLL, TModel> : ZMPage
        where TModel : BaseModel, new()
        where TBLL : BaseBLL<TModel>, new()
    {

        protected Control baseBottomBar;
        protected virtual Control BaseBottomBar
        {
            get
            {
                RegBottomBar();
                return baseBottomBar;
            }
        }

        protected Control baseEditContainer;
        protected virtual Control BaseEditContainer
        {
            get
            {
                RegEditContainer();
                return baseEditContainer;
            }
        }

        protected virtual void RegBottomBar()
        {
            if (baseBottomBar == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseBottomBar = (Control)Page.Master.Master.FindControl("contentPlaceHolder").FindControl("bottomBar");
                            break;
                        case 2:
                            baseBottomBar = (Control)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("bottomBar");
                            break;
                        default:
                            baseBottomBar = (Control)Page.Master.Master.FindControl("contentPlaceHolder").FindControl("bottomBar");
                            break;
                    }
                }
                else
                    baseBottomBar = Page.FindControl("bottomBar") as Control;
            }
        }
        protected virtual void RegEditContainer()
        {
            if (baseEditContainer == null)
            {
                if (Master != null)
                {
                    switch (ZConfig.TemplateLayer)
                    {
                        case 1:
                            baseEditContainer = (Control)Page.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder");
                            break;
                        case 2:
                            baseEditContainer = (Control)Page.Master.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder").FindControl("contentPlaceHolder");
                            break;
                        default:
                            baseEditContainer = (Control)Page.Master.Master.FindControl("contentPlaceHolder").FindControl("contentPlaceHolder");
                            break;
                    }
                }
                else
                    baseEditContainer = Page as Control;
            }
        }

        //protected override string PageCode
        //{
        //    get
        //    {
        //        if (IsNewModel)
        //            return base.PageCode;
        //        else
        //            return base.PageCode + ".Modify".ToUpper();
        //    }
        //}
        protected virtual string Id
        {
            get { return Request["ID"]; }
        }
        protected virtual string MarkName
        {
            get
            {
                return Request[""];
            }
        }
        protected virtual string ListURI
        {
            get
            {
                return "Default.aspx?Query=" + Query;
            }
        }

        protected string Flag
        {
            get
            {
                return Request["Flag"];
            }
        }

        protected virtual string AlertFlag
        {
            get
            {
                return "1";
            }
        }

        protected virtual TModel Model { get; set; }

        protected virtual bool IsNewModel
        {
            get { return string.IsNullOrEmpty(Id); }
        }

        protected override void RegControls()
        {
            base.RegControls();
            InitBottomBar();
        }

        protected virtual void InitBottomBar()
        {
            InitBottomBarSave();
            InitBottomBarCancel();
        }

        protected virtual void InitBottomBarSave()
        {
            if (BaseBottomBar != null)
            {
                ImageButton ibtnSave = new ImageButton();
                ibtnSave.ID = "ToolBarDetermine";
                ibtnSave.ImageUrl = ZPath.ImagesURI + "ToolBar" + GetLanguage() + "ImgDetermine.gif";
                ibtnSave.Click += new ImageClickEventHandler(OnBtnSaveClick);
               // ibtnSave.Attributes.Add("onclick", "OnSendSubmit(this);");
                InitBottomBarSave(ibtnSave);
                BaseBottomBar.Controls.Add(ibtnSave);
            }
        }

        protected virtual void InitBottomBarSave(ImageButton btnSave)
        {
        }

        protected virtual void InitBottomBarCancel()
        {
            if (BaseBottomBar != null)
            {
                ImageButton ibtnCancel = new ImageButton();
                ibtnCancel.ID = "ToolBarCancel";
                ibtnCancel.ImageUrl = ZPath.ImagesURI + "ToolBar" + GetLanguage() + "ImgCancel.GIF";

                if (!string.IsNullOrEmpty(Request["Flag"]) && (Request["Flag"] == "1"))
                    ibtnCancel.Attributes.Add("onclick", "parent.opener=null;window.close();");
                else
                    ibtnCancel.Attributes.Add("onclick", Server.HtmlDecode("location.href='" + ListURI + "'; return false;"));
                InitBottomBarCancel(ibtnCancel);
                BaseBottomBar.Controls.Add(ibtnCancel);
            }
        }

        protected virtual void InitBottomBarCancel(ImageButton ibtnCancel)
        {

        }

        protected override void OnBinding()
        {
            base.OnBinding();
            InitModel();
        }
        protected override bool IsNeedBind
        {
            get
            {
                if (!IsNewModel)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        protected virtual void InitModel()
        {
            if (!IsNewModel)
            {
                TBLL bll = new TBLL();
                Model = bll.GetModel(Id);
                bll.Dispose();
            }
            else
            {
                Model = new TModel();
            }
            if (Model == null)
            {
                OnError();
            }
        }
        protected override void OnBind()
        {
            if (Model != null)
            {
                ZBinder.BindModelToControls<TModel>(Model, BaseEditContainer);
            }
        }
        protected override void OnSubmit()
        {
            base.OnSubmit();
            try
            {
                if (!Validator())
                {
                    return;
                }
                OnSaving();
                OnSave();
                OnSaved();
            }
            catch (Exception ex)
            {
                if (ex.InnerException != null)
                {
                    ZJScript.Alert(ex.InnerException.Message.Replace("'", "").Replace("\r\n", ""));
                }
                else
                {
                    ZJScript.Alert(ex.Message.Replace("'", "").Replace("\r\n",""));
                }
                OnError(ex, "OnSubmit");
                //Response.End();
            }
        }
        protected virtual void OnBtnSaveClick(object sender, ImageClickEventArgs e)
        {
            OnSubmiting();
            OnSubmit();
            OnSubmited();
        }
        protected virtual bool Validator()
        {
            return true;
        }
        protected virtual void OnSave()
        {
            TBLL bll = new TBLL();
            bll.InsertOrUpdate(Model, "*");
            bll.Dispose();
        }

        protected virtual void OnAchieve()
        {
            if (!string.IsNullOrEmpty(Id))
            {
                Model.SetIsNew(false);
                CModel.SetProValue(Model, Model.GetPrimaryKey(), Id);
            }
            ZBinder.BindControlsToModel<TModel>(Model, this.BaseEditContainer);
        }
        protected virtual void OnAchieved() { }
        protected virtual void OnAchieving()
        {
            if (Model == null)
            {
                Model = new TModel();
            }
        }
        protected virtual void OnSaved()
        {
            if (!string.IsNullOrEmpty(Flag) && (Flag == "1"))
            {
                ZJScript.CloseWindow();
            }
            else
            {
                Thread.Sleep(ZSetup.SleepTimes);
                switch (AlertFlag)
                {
                    case "1":
                        ZJScript.AlertAndRedirect("保存成功", ListURI);
                        break;
                    default:
                        this.Response.Redirect(ListURI, true);
                        break;
                }
            }
        }
        protected virtual void OnSaving()
        {
            OnAchieving();
            OnAchieve();
            OnAchieved();
        }
        protected override void OnLoading()
        {
            base.OnLoading();

            if (ZSetup.IFUseLanguage)
            {
                foreach (Control control in BaseEditContainer.Controls)
                {
                    if (control is HtmlInputText)
                    {
                        HtmlInputText con = control as HtmlInputText;
                        string des = con.Attributes["des"];
                        if (!string.IsNullOrEmpty(des))
                        {
                            con.Attributes["des"] = ZLanguages.GetLanguages(con.Attributes["des"]);
                        }

                    }
                }
            }

        }
    }
}
